<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\admin\model\Note as NoteModel;
use think\Request;
use think\Session;

class Note extends Controller
{
    // 初始化 防止用户未登录就访问页面
    public function _initialize()
    {
        if (!session('nodeList')) {
            return $this->error('您还未登录,请登录',url('admin/index/index'));
        }
    }
    // 笔记列表
    public function index()
    {
        $auth = request()->controller().request()->action();
        $this->judge($auth);

        $data = $this->getAllNote();
//        halt($data);
        return $this->fetch('note/index', [
            'data' => $data
        ]);
    }


    // 笔记详情
    public function noteDetails($id)
    {
        $auth = request()->controller().request()->action();
        $this->judge($auth);

        // 该笔记的类型
        $type = $this->getType($id);

        // 判断笔记是什么类型的
        switch ($type) {
            case '0':
                // 文章
                $titleList = $this->getTitleDetails($id);
                return view('note/title',[
                    'data' => $titleList
                ]);
                break;
            case '1':
                // 图片
                $imageList = $this->getImageDetails($id);
//                halt($imageList);
                return view('note/image',[
                    'data' => $imageList
                ]);
                break;
            case '2':
                // 视频
                $videoList = $this->getVideoDetails($id);
//                halt($videoList);
                return view('note/video',[
                    'data' => $videoList
                ]);
                break;
        }


    }


    // 是否上推荐
    public function isTop()
    {
        $auth = request()->controller().request()->action();
        $this->judge($auth);

        $this->checkMethod('put');
        // 获取修改的id
        $data = input('put.');
        $note = NoteModel::get($data['id']);
        // 要修改的值
        $top = $note->top == '0' ? '1': '0';
        // 修改
        $result = $note->save(['top' => $top], $data);
//        $result = true;
//        halt($top);
        if ($result) {
            return true;
        } else {
            return false;
        }

    }



    // 获取所有所有笔记信息
    protected function getAllNote()
    {
        $data = Db::field('note.id,note.title,note.type,note.create_at,note.top,type.tag_name,u.nickname')
            ->table(['2B_user_note' => 'note', '2B_note_type' => 'type', '2B_user_info' => 'u'])
            ->where('note.tags_id = type.id')
            ->where('note.uid = u.uid')
            ->order('id')
            ->paginate(10);

        return $data;
    }


    // 获取笔记类型
    protected function getType($id)
    {
        return Db::name('user_note')->field('type')->find($id)['type'];
    }

    // 获取文章类型的笔记详情
    protected function getTitleDetails($id)
    {
        $data = Db::field('note.title,note.thumb,note.type,note.create_at,note.top,note.content,note.praise,type.tag_name,u.nickname')
            ->table(['2B_user_note' => 'note', '2B_note_type' => 'type', '2B_user_info' => 'u'])
            ->where('note.id',$id)
            ->where('note.tags_id = type.id')
            ->where('note.uid = u.uid')
            ->find();

        return $data;
    }


    // 获取图片类型的笔记详情
    protected function getImageDetails($id)
    {
        $data = Db::field('note.id,note.title,note.thumb,note.type,note.create_at,note.top,note.content,note.praise,type.tag_name,group_concat(image.img) img')
            ->table(['2B_user_note' => 'note', '2B_note_type' => 'type','2B_note_image' => 'image'])
            ->where('note.id',$id)
            ->where('note.tags_id = type.id')
            ->where('image.note_id',$id)
            ->group('id')
            ->find();
        // 获取用户名
        $user = Db::field('u.nickname')
            ->table(['2B_user_note' => 'note', '2B_user_info' => 'u'])
            ->where('note.id',$id)
            ->where('note.uid = u.uid')
            ->find()['nickname'];

        // 追加用户到$data
        $data['user'] = $user;

        // 分割图片
        $data['img'] = explode(',',$data['img']);

        return $data;
    }


    // 获取视频类型的笔记详情
    protected function getVideoDetails($id)
    {
        $data = Db::field('note.title,note.thumb,note.type,note.create_at,note.top,note.content,note.praise,type.tag_name,video.video,u.nickname')
            ->table(['2B_user_note' => 'note', '2B_note_type' => 'type','2B_note_video' => 'video','2B_user_info' => 'u'])
            ->where('note.id',$id)
            ->where('note.tags_id = type.id')
            ->where('note.uid = u.uid')
            ->where('video.note_id',$id)
            ->find();

        return $data;
    }

    // 验证访问方式
    private function checkMethod($method)
    {
        switch ($method) {
            case 'post':
                if (!Request::instance()->isPost()) {
                    $this->redirect('',url('admin/Error/notFound'));
                    return;
                }
                break;
            case 'ajax':
                if (!Request::instance()->isAjax()) {
                    $this->redirect('',url('admin/Error/notFound'));
                    return;
                }
                break;
            case 'delete':
                if (!Request::instance()->isDelete()) {
                    $this->redirect('',url('admin/Error/notFound'));
                    return;
                }
                break;
            case 'put':
                if (!Request::instance()->isPut()) {
                    $this->redirect('',url('admin/Error/notFound'));
                    return;
                }
                break;
        }
    }

    /*
     * 判断权限
     */
    public function judge($auth)
    {
        $nodeList = Session::get('nodeList');
        if (empty($nodeList)) {
            return $this->error('您的权限不足!');
        }
        if (!in_array($auth,$nodeList)) {
            return $this->error('您的权限不足!');
        }
    }
}
